package com.wd.nio;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * @author long
 * @date 2013-5-13 上午10:52:38
 * @qq 108232706
 * @description SocketsTest
 */
public class SocketsTest {
	public static void main(String[] args) {

		// System.out.println(readFileByLines("d:/proxys.txt"));
		// writeFile("d:/1.txt", "aaa");
		//		Map<String, Integer> adds = getSockets("d:/proxys.txt");
		Map<String, Integer> adds = getSockets("d:/suc1.txt");
		Set<Entry<String, Integer>> set = adds.entrySet();
		System.out.println("==========总长度:" + set.size());
		String addr;
		int port;
		StringBuilder strs = new StringBuilder("=========成功的代理\r\n");
		int sucCount = 0;
		int verifyCount = 0;
		//设置连接主机的超时时间 ms
		//		System.setProperty("sun.net.client.defaultConnectTimeout", "3000");
		//设置从主机读取数据的超时时间
		//		System.setProperty("sun.net.client.defaultReadTimeout", "3000");
		long cost;
		for (Entry<String, Integer> entry : set) {
			addr = entry.getKey();
			port = entry.getValue();
			boolean suc = verifySocket(addr, port);
			cost = end - start;
			System.out.println(addr + ":" + port + " " + suc + " cost ms:" + cost);
			verifyCount++;
			if (suc) {
				sucCount++;
				strs.append(addr + ":" + port + " cost ms: " + cost + "\r\n");
			}
			System.out.println("==verifyCount:" + verifyCount + " suc count:" + sucCount);
		}
		strs.append("===========verifyCount:" + verifyCount + " suc count:" + sucCount);
		System.out.println(strs.toString());
		writeFile("d:/suc.txt", strs.toString());
	}

	public static Map<String, Integer> getSockets(String fileName) {
		Map<String, Integer> adds = new HashMap<String, Integer>();
		BufferedReader reader = null;
		String[] arrStr;
		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "GB2312"));
			String tempString = null;

			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				arrStr = tempString.split(":");
				if (arrStr.length > 1)
					adds.put(arrStr[0], Integer.valueOf(arrStr[1].split("\t")[0]));
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
		return adds;
	}

	static long start, end;

	public static boolean verifySocket(String addr, int port) {

		boolean suc = true;
		Socket socket;
		try {
			start = System.currentTimeMillis();
			SocketAddress soceketAddr = new InetSocketAddress(addr, port);
			socket = new Socket();
			socket.connect(soceketAddr, 2000);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			suc = false;
		} catch (IOException e) {
			e.printStackTrace();
			suc = false;
		}
		end = System.currentTimeMillis();
		return suc;
	}

	public static String readFileByLines(String fileName) {

		BufferedReader reader = null;
		StringBuilder stringBuilder = new StringBuilder();
		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "GB2312"));
			String tempString = null;

			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				stringBuilder.append(tempString + "\r\n");
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
		return stringBuilder.toString();
	}

	public static void writeFile(String fileName, String content) {
		PrintWriter out = null;
		try {
			out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "GB2312")));
			out.write(content);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (out != null)
				out.close();
		}
	}
}
